﻿using AutoMapper;
using CodeSaude.Application.Interfaces;
using CodeSaude.Application.ViewModels;
using CodeSaude.Domain.Entities;
using CodeSaude.Domain.Interfaces.Service;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeSaude.Application.Services
{
    public class ProntuarioApplicationService : BaseApplicationService, IProntuarioApplicationService
    {
        private readonly IProntuarioService _prontuarioService;

        public ProntuarioApplicationService(IProntuarioService prontuarioService)
        {
            _prontuarioService = prontuarioService;
        }

        public void Create(ProntuarioViewModel model)
        {
            try
            {
                Prontuario prontuario = Mapper.Map<ProntuarioViewModel, Prontuario>(model);

                BeginTransaction();
                _prontuarioService.Add(prontuario);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Dispose()
        {
            _prontuarioService.Dispose();
        }

        public IEnumerable<ProntuarioViewModel> GetAll()
        {
            try
            {
                return Mapper.Map<IEnumerable<ProntuarioViewModel>>(_prontuarioService.GetAll());
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ProntuarioViewModel GetById(int id)
        {
            try
            {
                return Mapper.Map<ProntuarioViewModel>(_prontuarioService.GetById(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Delete(ProntuarioViewModel model)
        {
            try
            {
                Prontuario prontuario = _prontuarioService.GetById(model.ProntuarioId);
                BeginTransaction();
                _prontuarioService.Remove(prontuario);
                Commit();

            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Update(ProntuarioViewModel model)
        {
            try
            {
                Prontuario prontuario = Mapper.Map<Prontuario>(model);
                BeginTransaction();
                _prontuarioService.Update(prontuario);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ProntuarioViewModel GetById(int idProntuario, int idEspecialidade)
        {
            try
            {
                ProntuarioViewModel obj = new ProntuarioViewModel();
                obj = Mapper.Map<ProntuarioViewModel>(_prontuarioService.GetAll().Where(x => x.ProntuarioId.Equals(idProntuario) && x.Especialidade.EspecialidadeId.Equals(idEspecialidade)).FirstOrDefault());
                return obj;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
